package com.hn.shoutchinese.sentence.service.impl;

import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONArray;

import com.hn.comm.Tools;
import com.hn.net.Kit;
import com.hn.shoutchinese.sentence.model.SentenceCombin;
import com.hn.shoutchinese.sentence.service.ISentenceService;

public class SentenceServiceImpl implements ISentenceService {

	public List<SentenceCombin> getSentences(String chinese, String english,
			int num) {

		List<SentenceCombin> backSentences = new ArrayList<SentenceCombin>();

		// http://dict.baidu.com/s?wd=%D7%D4%BC%BAself&tn=dict&dt=example
		String head = "http://dict.baidu.com/s?wd=";
		String url = null;
		try {
			String word = java.net.URLEncoder.encode(chinese + english,
					"gb2312");
			url = head + word + "&tn=dict&dt=example";
		} catch (Exception e) {
			e.printStackTrace();
		}
		String content = Kit.httpRequest(url, null, "GET", "gb2312");

		// String content = Tools
		// .readFile("D:\\eclipse_workspace\\indigo\\shoutchinese-robot\\res\\sentence.htm");

		String strIndexKey = "var examples =";
		String strEndKey = "</script>";
		int iStartIndex = content.indexOf(strIndexKey) + strIndexKey.length();
		int iEndIndex = content.indexOf(strEndKey, iStartIndex);

		String sentenceJson = content.substring(iStartIndex, iEndIndex);

		JSONArray arrsentenceJsonObj = JSONArray.fromObject(sentenceJson);

		// 句子
		int iLength = arrsentenceJsonObj.size();
		if (iLength > num) {
			iLength = num;
		}
		for (int i = 0; i < iLength; i++) {
			SentenceCombin sc = new SentenceCombin();
			StringBuffer strChinese = new StringBuffer();
			StringBuffer strEnglish = new StringBuffer();
			Object sentence = arrsentenceJsonObj.get(i);
			
			JSONArray sentenceFirstChinese = (JSONArray) ((JSONArray) sentence)
					.get(0);
			
			String firstChinese=((JSONArray)sentenceFirstChinese.get(0)).get(0).toString();
			
			boolean isAnti=Tools.isChinese(firstChinese);
			
			int chineseIndex=0;
			int englishIndex=1;
			if(!isAnti)
			{
				chineseIndex=1;
				englishIndex=0;
			}
			
			
			// 中文
			JSONArray sentenceChinese = (JSONArray) ((JSONArray) sentence)
					.get(chineseIndex);
			// 得到中文
			for (int s = 0; s < sentenceChinese.size(); s++) {
				JSONArray word = (JSONArray) sentenceChinese.get(s);
				strChinese.append(word.get(0).toString());
			}

			// 英文
			JSONArray sentenceEnglish = (JSONArray) ((JSONArray) sentence)
					.get(englishIndex);
			// 得到英文
			for (int e = 0; e < sentenceEnglish.size(); e++) {
				JSONArray word = (JSONArray) sentenceEnglish.get(e);
				strEnglish.append(word.get(0).toString());
				if (e < sentenceEnglish.size() - 2) {
					strEnglish.append(" ");
				}
			}
			// System.out.println(sentence);
			// 判断是否中英文对调
//			if (Tools.isChinese(strChinese.toString()) ) {
				sc.setSourceSen(strEnglish.toString());
				sc.setTranSen(strChinese.toString());
//			} else {
//				sc.setSourceSen(strChinese.toString());
//				sc.setTranSen(strEnglish.toString());
//			}
			backSentences.add(sc);
		}

		return backSentences;
	}

	public static void main(String[] args) {
		SentenceServiceImpl ss = new SentenceServiceImpl();
		List<SentenceCombin> arrSC = ss.getSentences("自己", "self", 3);
		for (SentenceCombin sc : arrSC) {
			System.out.println("英文:" + sc.getSourceSen());
			System.out.println("中文:" + sc.getTranSen());
			System.out.println("---------------------------------");
		}
		// System.out.println(ss);
	}

}
